package org.hackystat.devcathlon.ui.page.matches.manage.matchconfigurationpanel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.hackystat.devcathlon.configuration.ConfigurationManager;
import org.hackystat.devcathlon.engine.event.Event;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.ui.page.matches.manage.MatchesManagePage;
import org.hackystat.utilities.tstamp.Tstamp;

/**
 * The configuration for matches in the manage matches page.
 * @author robin
 *
 */
public class MatchConfigurationPanel extends Panel {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /** Event name in the system. */
  private List<Event> newEvents = new ArrayList<Event>();
  private List<String> oldEvents = new ArrayList<String>();
  private String matchName = "";
  
  /**
   * Constructor for the match configuration panel for the match manage page.
   * @param id Component id.
   * @param mItem The match item.
   * @throws Exception If there is any errors with Tstamp.
   */
  public MatchConfigurationPanel(String id, Match mItem) throws Exception {
    super(id);
    XMLGregorianCalendar now = Tstamp.makeTimestamp();
    String startStringTime = " ";
    String endStringTime = "";
    String status = "";
    
    try {
      startStringTime = mItem.printStartDateTime();
      endStringTime = mItem.printEndDateTime(); 
      if (Tstamp.lessThan(now, mItem.getEndDate())) {
        status = "On Going";
      }
      else {
        status = "Completed";
      }
      
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    setMatchName(mItem.getName());
    // Add labels.
    Form form = new Form("configForm");
    add(form);
    form.add(new Label("StatusLabel", status));
    form.add(new Label("MatchLabel", mItem.getName()));
    form.add(new Label("startTimeLabel", startStringTime));
    form.add(new Label("endTimeLabel", endStringTime));
    
    Iterator<Team> teams = mItem.getTeams().iterator();
    
    form.add(new Label("teams1Label", teams.next().getName()));
    form.add(new Label("teams2Label", teams.next().getName()));
    
    // Get list of events already in the match.
    List<Event> eventItem = mItem.getEvents();
    List<String> matchEventNames = new ArrayList<String>();
    for (Event events : eventItem) {
      matchEventNames.add(events.getName());
    }
    
    // List of all the events in the system.
    final List<String> eventLocations = ConfigurationManager.getInstance().getAllEventLocations();
    final List<String> eventNames = new ArrayList<String>();
    for (String eventLocation : eventLocations) {
      try {
        Event eClass = (Event) (Class.forName(eventLocation).newInstance());
        eventNames.add(eClass.getName());
        
      }
      catch (Exception e) {
        e.printStackTrace();
      }      
    }

    // Add the list of events.
    final CheckBoxMultipleChoice checkEvents = new CheckBoxMultipleChoice("newEvents", 
        new PropertyModel(this, "oldEvents"), eventNames);
    form.add(checkEvents);
    
    // Have the events already in the match checked.
    this.getOldEvents().clear();
    //this.getEvenTypes().addAll(matchEventNames);
    this.getOldEvents().addAll(matchEventNames);
    
    // Add button to the form.
    form.add(new Button("submitButton") {
      /** Support serialization */
      private static final long serialVersionUID = 1L;
      
      @Override
      public void onSubmit() {
        try {
          // Add events for the match.
          newEvents.clear();
          for (String event : checkEvents.getInputAsArray()) {
            String eventLoc = eventLocations.get(Integer.valueOf(event));
            Event userSelect = (Event) (Class.forName(eventLoc).newInstance());
            newEvents.add(userSelect);
          }
          MatchManager.getInstance().reconfigEvents(matchName, newEvents);
          setResponsePage(new MatchesManagePage());
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        
      }
    });
      
  }
  
  /**
   * Sets the event list for new events.
   * @param event The list of events.
   */
  public void setNewEvents(List<Event> event) {
    this.newEvents = event;
  }
  
  /**
   * Gets the list of events for new events.
   * @return The list of events.
   */
  public List<Event> getNewEvents() {
    return this.newEvents;
  }
  
  /**
   * Sets the list of events for old events.
   * @param events The list of events.
   */
  public void setOldEvents(List<String> events) {
    this.oldEvents = events;    
  }
  
  /**
   * Gets the list of events already selected.
   * @return The list of events.
   */
  public List<String> getOldEvents() {
    return this.oldEvents;
  }
  
  /**
   * Gets the name of the match.
   * @return The match name.
   */
  /*
  private String getMatchName() {
    return this.matchName;
  }
  */
  
  /**
   * Sets the name of the match.
   * @param name The match name.
   */
  private void setMatchName(String name) {
    this.matchName = name;
  }
}
